package com.dmap.admin.controller.background;

import com.dmap.admin.controller.background.param.RolePermissionParam;
import com.dmap.admin.service.backgroundv1.RolePermissionService;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 08/12/17 Time: 11:28
 * \* Description:
 * \
 */
@Api(value = "角色权限关系管理", description = "background/角色权限关系管理")
@Controller
@RequestMapping(value = "/background/pmpd-admin-server/v1/RolePermission")
public class RolePermissionController {

    @Resource
    private RolePermissionService rolePermissionService;

    @ResponseBody
    @ApiOperation(value = "为角色赋予权限",
            notes = "1.数组类型批量插入;" +
                    "2.必填： roleId, permissionId")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public BaseVo insert(@RequestBody List<RolePermissionParam> rolePermissionParamList) {
        BaseVo baseVo = new BaseVo();
        if (CollectionUtils.isEmpty(rolePermissionParamList)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        rolePermissionService.deletePermissionListByRoleId(Long.valueOf(rolePermissionParamList.get(0).getRoleId()));
        for (RolePermissionParam param : rolePermissionParamList) {
            Long roleId = Long.valueOf(param.getRoleId());
            Long permissionId = Long.valueOf(param.getPermissionId());
            rolePermissionService.insert(roleId, permissionId);
        }
        return baseVo;
    }

}